# coding: utf-8
from __future__ import division
from . geo_classes import Region, Node


class SegRegion(Region):
    def __init__(self, left_up, right_down, lat_n, lng_n):
        """传入坐标一律wgs84"""
        super(SegRegion, self).__init__(left_up, right_down)
        self.lat_n = lat_n
        self.lng_n = lng_n
        self.lat_interval = (left_up.lat - right_down.lat) / lat_n
        self.lng_interval = (right_down.lng - left_up.lng) / lng_n
        """self.map是从上到下，从左到右的region"""
        self.map = []
        for i in range(lat_n):
            tmp = []
            for j in range(lng_n):
                tmp.append(Region(
                    Node(lat=left_up.lat - self.lat_interval * i, lng=left_up.lng + self.lng_interval * j),
                    Node(lat=left_up.lat - self.lat_interval * (i + 1), lng=left_up.lng + self.lng_interval * (j + 1))
                ))
            self.map.append(tmp)

    def restaurant_y_feature(self, y_file):
        with open(y_file) as f:
            for line in f:
                line = line.split(' ')
                lat = float(line[-2])
                lng = float(line[-3])
                review_num = int(line[-6]) + 1
                node = Node(lat=lat, lng=lng)
                # print(lat, lng, self.left_up.lat, self.left_up.lng, self.right_down.lat, self.right_down.lng)
                if node.in_(self):
                    i = int((self.left_up.lat - lat) / self.lat_interval)
                    j = int((lng - self.left_up.lng) / self.lng_interval)
                    self.map[i][j].y += review_num
                    print(i, j , self.map[i][j].y)
